package com.diamondfsd.jooq.methods;

import com.diamondfsd.jooq.learn.codegen.tables.records.S1UserRecord;
import org.jooq.DSLContext;

import java.util.ArrayList;
import java.util.List;

import static com.diamondfsd.jooq.learn.codegen.Tables.S1_USER;

public class DeleteMethod {

  public static void deleteByDSL(DSLContext dslContext){
    dslContext.delete(S1_USER).where(S1_USER.USERNAME.eq("demo1")).execute();
  }

  public static void deleteByRecord(DSLContext dslContext){
    S1UserRecord record = dslContext.newRecord(S1_USER);
    record.setId(2);
    int deleteRows = record.delete();
// deleteRows = 1
// SQL: delete from `learn-jooq`.`s1_user` where `learn-jooq`.`s1_user`.`id` = 2

    S1UserRecord record2 = dslContext.newRecord(S1_USER);
    record2.setUsername("demo1");
    int deleteRows2 = record2.delete();
// deleteRows == 0
// SQL: delete from `learn-jooq`.`s1_user` where `learn-jooq`.`s1_user`.`id` is null
  }

  /**
   * 批量删除
   */
  public static void deleteByBatch(DSLContext dslContext){
    S1UserRecord record1 = new S1UserRecord();
    record1.setId(1);
    S1UserRecord record2 = new S1UserRecord();
    record2.setId(2);
    dslContext.batchDelete(record1, record2).execute();
    //==============//
    List<S1UserRecord> recordList = new ArrayList<>();
    recordList.add(record1);
    recordList.add(record2);
    dslContext.batchDelete(recordList).execute();
  }
}
